/*
 *  Jiki - a small lightweight wiki engine.
 *
 *  Copyright 2008 Peter Halbmayer <phalbmayer@gmail.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

package at.pmsa.jiki.conn.http;

public class HTTPRequest{
	
	private String command;

	private String path;

	private String host;

	private int port;

	private String protocol;

	private String version;

	private String userAgent;

	private String accept;

	private String acceptLanguage;

	private String acceptEncoding;

	private String acceptCharset;

	private int keepAlive;

	private String connection;
	
	private String referer;
	
	private String pragma;
	
	private String  cacheControl;
	
	private String contentType;
	
	private int contentLength = 0;

	private String content;

	public HTTPRequest(Object[] request){
		
		System.out.println("new HttpRequest size: " + request.length);
		
		for(int i = 0; i < request.length; i++){
			String requestLine = (String)request[i];
			
//			System.out.println("requestLine: " + requestLine);
			
			if(requestLine.startsWith("GET")){
				command = "GET";
				String[] parts = requestLine.trim().split(" ");
				path = parts[1].trim();
				String[] protocolVersion = parts[2].trim().split("/");
				protocol = protocolVersion[0].trim();
				version = protocolVersion[1].trim();
			}
			//in the case where the save button is pressed
			else if(requestLine.startsWith("POST")){
				command = "POST";
//				System.out.println("requestLine: " + requestLine + "\n");
				String[] parts = requestLine.trim().split(" ");
				path = parts[1].trim();
				String[] protocolVersion = parts[2].trim().split("/");
				protocol = protocolVersion[0].trim();
				version = protocolVersion[1].trim();
			}
			else{
				String[] parts = requestLine.trim().split(":");
				if(parts[0].trim().equalsIgnoreCase("Host")){
					host = parts[1].trim();
					try{
						port = Integer.parseInt(parts[2].trim());
					}
					catch(IndexOutOfBoundsException e){
						port = 80;
					}
				}
				else if( parts[0].trim().equalsIgnoreCase("User-Agent")){
					StringBuilder builder = new StringBuilder(parts[1].trim());
					for(int j = 2; j < parts.length; j++){
						builder.append(":").append(parts[j].trim());
					}
					userAgent = builder.toString();
				}
				else if(parts[0].trim().equalsIgnoreCase("Accept")){
					accept = parts[1].trim();
				}
				else if(parts[0].trim().equalsIgnoreCase("Accept-Language")){
					acceptLanguage = parts[1].trim();
				}
				else if( parts[0].trim().equalsIgnoreCase("Accept-Encoding")){
					acceptEncoding = parts[1].trim();
				}
				else if(parts[0].trim().equalsIgnoreCase("Accept-Charset")){
					acceptCharset = parts[1].trim();
				}
				else if(parts[0].trim().equalsIgnoreCase("Keep-Alive")){
					keepAlive = Integer.parseInt(parts[1].trim());
				}
				else if(parts[0].trim().equalsIgnoreCase("Connection")){
					connection = parts[1].trim();
				}
				else if(parts[0].trim().equalsIgnoreCase("Referer")){
					System.out.println("Referer parts: " + parts.length);
					for(int z = 0; z < parts.length; z++){
						System.out.println(parts[z]);
					}
					if(parts.length == 3)
						referer = parts[1].trim() + ":" + parts[2].trim();
					else
						referer = null;
				}
				else if(parts[0].trim().equalsIgnoreCase("Pragma")){
					pragma = parts[1].trim();
				}
				else if(parts[0].trim().equalsIgnoreCase("Cache-control")){
					cacheControl = parts[1].trim();
				}
				else if(parts[0].trim().equalsIgnoreCase("Content-Type")){
					contentType = parts[1].trim();
				}
				else if(parts[0].trim().equalsIgnoreCase("Content-Length")){
					contentLength = new Integer(parts[1].trim()).intValue();
				}
			}
		}
	}
	
	public String getHost(){
		return host;
	}
	
	public int getPort(){
		return port;
	}
	
	public void setPath(String p){
		path = p;
	}

	public String getPath(){
		return path;
	}
	
	public String getCommand(){
		return command;
	}

	public String getContentType() {
		return contentType;
	}
	
	public int getContentLength(){
		return contentLength;
	}
	
	public void setContent(String b){
		content = b;
	}
	
	public String getContent(){
		return content;
	}
	
	public String printReq(){
		StringBuilder builder = new StringBuilder(command + " ");
		
		builder.append(path).append(" ").append(protocol).append("/").append(version).append("\r\n");
		builder.append("Host: ").append(host).append(":").append(port).append("\r\n");		
		return builder.toString();
	}

	public String toString(){
//		StringBuilder builder = new StringBuilder("GET ");
		StringBuilder builder = new StringBuilder(command + " ");
		builder.append(path).append(" ").append(protocol).append("/").append(version).append("\r\n");
		builder.append("Host: ").append(host).append(":").append(port).append("\r\n");
		builder.append("User-Agent: ").append(userAgent).append("\r\n");
		builder.append("Accept: ").append(accept).append("\r\n");
		builder.append("Accept-Language: ").append(acceptLanguage).append("\r\n");
		builder.append("Accept-Encoding: ").append(acceptEncoding).append("\r\n");
		builder.append("Accept-Charset: ").append(acceptCharset).append("\r\n");
		builder.append("Keep-Alive: ").append(keepAlive).append("\r\n");
		builder.append("Connection: ").append(connection).append("\r\n");
		builder.append("Referer: ").append(referer).append("\r\n");
		builder.append("Cache-control: ").append(cacheControl).append("\r\n");
		builder.append("Pragma: ").append(pragma).append("\r\n");
		builder.append("Content-Type: ").append(contentType).append("\r\n");
		builder.append("Content-Length: ").append(contentLength).append("\r\n");
		
		if( content != null && content.length() > 0){
			builder.append("Content-Length: ").append(content.toString().getBytes().length).append("\r\n");
			builder.append("Content-Type: ").append(getContentType()).append("\r\n");
			builder.append("\r\n").append(content);
			builder.append("\r\n");
		}
		
		return builder.toString();
	}
	
//	public String toString(){
////		StringBuilder builder = new StringBuilder("GET ");
//		StringBuilder builder = new StringBuilder(command + " ");
//		builder.append(path).append(" ").append(protocol).append("/").append(version).append("\r\n");
//		builder.append("Host: ").append(host).append(":").append(port).append("\r\n");
//		builder.append("User-Agent: ").append(userAgent).append("\r\n");
//		builder.append("Accept: ").append(accept).append("\r\n");
//		builder.append("Accept-Language: ").append(acceptLanguage).append("\r\n");
//		builder.append("Accept-Encoding: ").append(acceptEncoding).append("\r\n");
//		builder.append("Accept-Charset: ").append(acceptCharset).append("\r\n");
//		builder.append("Keep-Alive: ").append(keepAlive).append("\r\n");
//		builder.append("Connection: ").append(connection).append("\r\n");
//		builder.append("Referer: ").append(referer).append("\r\n");
//		builder.append("Cache-control: ").append(cacheControl).append("\r\n");
//		builder.append("Pragma: ").append(pragma).append("\r\n");
//		builder.append("Content-Type: ").append(contentType).append("\r\n");
//		builder.append("Content-Length: ").append(contentLength).append("\r\n");
//		
//		if( content != null && content.length() > 0){
//			builder.append("Content-Length: ").append(content.toString().getBytes().length).append("\r\n");
//			builder.append("Content-Type: ").append(getContentType()).append("\r\n");
//			builder.append("\r\n").append(content);
//			builder.append("\r\n");
//		}
//		
//		return builder.toString();
//	}
}
